#include "../include/Transaction.hh"
#include "../include/Account.hh" // 为了使用CurrencyConverter
#include <iostream>
#include <iomanip>
#include <ctime>

Transaction::Transaction(std::string id, TransactionType t, double amt, 
                         const std::string& src, const std::string& dest, 
                         const std::string& n) 
    : transactionId(id), type(t), amount(amt),
      sourceAccountId(src), destinationAccountId(dest), note(n), currency(Currency::CNY) {
    
    // 记录当前时间作为交易时间戳
    timestamp = std::time(nullptr);
}

// 添加带货币类型的构造函数实现
Transaction::Transaction(std::string id, TransactionType t, double amt, 
                         const std::string& src, const std::string& dest, 
                         const std::string& n, Currency curr) 
    : transactionId(id), type(t), amount(amt),
      sourceAccountId(src), destinationAccountId(dest), note(n), currency(curr) {
    
    // 记录当前时间作为交易时间戳
    timestamp = std::time(nullptr);
}

void Transaction::display() const {
    // 格式化时间
    char timeStr[100];
    std::strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", std::localtime(&timestamp));
    
    std::cout << "交易ID: " << transactionId << std::endl;
    
    // 显示交易类型
    std::cout << "类型: ";
    switch (type) {
        case TransactionType::DEPOSIT:
            std::cout << "存款";
            break;
        case TransactionType::WITHDRAW:
            std::cout << "取款";
            break;
        case TransactionType::TRANSFER:
            std::cout << "转账";
            break;
        case TransactionType::CURRENCY_EXCHANGE:
            std::cout << "货币兑换";
            break;
    }
    std::cout << std::endl;
    
    // 获取货币符号
    std::string currencySymbol = CurrencyConverter::getCurrencySymbol(currency);
    
    std::cout << "金额: " << currencySymbol << std::fixed << std::setprecision(2) << amount << std::endl;
    std::cout << "货币: " << CurrencyConverter::getCurrencyName(currency) << std::endl;
    std::cout << "时间: " << timeStr << std::endl;
    
    // 根据交易类型显示适当的账户信息
    switch (type) {
        case TransactionType::DEPOSIT:
            std::cout << "存入账户: " << destinationAccountId << std::endl;
            break;
        case TransactionType::WITHDRAW:
            std::cout << "从账户: " << sourceAccountId << std::endl;
            break;
        case TransactionType::TRANSFER:
            std::cout << "从账户: " << sourceAccountId << std::endl;
            std::cout << "到账户: " << destinationAccountId << std::endl;
            break;
        case TransactionType::CURRENCY_EXCHANGE:
            std::cout << "账户: " << sourceAccountId << std::endl;
            break;
    }
    
    // 显示备注（如果有）
    if (!note.empty()) {
        std::cout << "备注: " << note << std::endl;
    }
    
    std::cout << "----------------------------" << std::endl;
}

std::string Transaction::getTypeString() const {
    switch (type) {
        case TransactionType::DEPOSIT:
            return "存款";
        case TransactionType::WITHDRAW:
            return "取款";
        case TransactionType::TRANSFER:
            return "转账";
        case TransactionType::CURRENCY_EXCHANGE:
            return "货币兑换";
        default:
            return "未知类型";
    }
} 